package com.dg.river.core.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dg.river.R;
import com.dg.river.core.util.EmojiFilterUtils;
import com.dg.river.core.util.TextUtil;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.List;

/**
 * Created by hy on 2018/10/16
 * Description : 列表dialog  从底部弹出
 */
public class ListBottomDialog extends Dialog {

    private static ListBottomDialog dialog;

    private static RecyclerView recyclerView;

    private static boolean isDismiss = false;

    private static TextView tv_cancel;

    private static ListDialogAdapter adapter;

    public static List<ListDialogItemBean> listData;
    /**
     * 点击事件
     */
    private static OnItemClickListener mOnItemClickListener;

    /**
     * ItemClick的回调接口
     */
    public interface OnItemClickListener {
        void onItemClick(ListBottomDialog dialog, BaseQuickAdapter adapter, View view, int position);

        /**
         * 点击取消
         */
        void onClickCancel();
    }

    /**
     * 设置点击事件
     *
     * @param mOnItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }


    public ListBottomDialog(@NonNull Context context) {
        super(context);
    }

    public ListBottomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    /**
     * 弹出自定义Dialog
     *
     * @param context 上下文
     * @return
     */
    public static ListBottomDialog show(Context context, boolean cancel, List<ListDialogItemBean> data) {
        dialog = new ListBottomDialog(context);
        dialog.setContentView(R.layout.dialog_list);
        // 按返回键是否取消
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setAttributes(lp);
        //设置该属性，dialog可以铺满屏幕
        dialog.getWindow().setBackgroundDrawable(null);

        recyclerView = dialog.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        adapter = new ListDialogAdapter(R.layout.dialog_list_bottom_item, data);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                slideToDown(dialog.getWindow().findViewById(R.id.bottom_dialog_layout));
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(dialog, adapter, view, position);
                }
            }
        });

        tv_cancel = dialog.findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                slideToDown(dialog.getWindow().findViewById(R.id.bottom_dialog_layout));
            }
        });
        isDismiss = false;
        dialog.show();
        slideToUp(dialog.getWindow().findViewById(R.id.bottom_dialog_layout));
        return dialog;
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        if (isOutOfBounds(getContext(), event)) {
            if (!isDismiss) {
                slideToDown(dialog.getWindow().findViewById(R.id.bottom_dialog_layout));
                isDismiss = true;
            }
        }
        return false;
    }

    private boolean isOutOfBounds(Context context, MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View decorView = getWindow().getDecorView();
        return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop))
                || (y > (decorView.getHeight() + slop));
    }

    /**
     * 弹出动画
     *
     * @param view
     */
    public static void slideToUp(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        slide.setDuration(300);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        view.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 弹回动画
     *
     * @param view
     */
    public static void slideToDown(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                .0f, Animation.RELATIVE_TO_SELF, 1.0f);
        slide.setDuration(200);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        view.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dialog.dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * item内容
     */
    public static class ListDialogItemBean implements Serializable {
        private String text;
        private int textColor;
        private boolean state = false;

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public int getTextColor() {
            return textColor;
        }

        public void setTextColor(int textColor) {
            this.textColor = textColor;
        }

        public boolean isState() {
            return state;
        }

        public void setState(boolean state) {
            this.state = state;
        }
    }

    /**
     * adapter
     */
    private static class ListDialogAdapter extends BaseQuickAdapter<ListDialogItemBean, BaseViewHolder> {

        public ListDialogAdapter(int layoutResId, @Nullable List<ListDialogItemBean> data) {
            super(layoutResId, data);
            listData = data;
        }

        @Override
        protected void convert(@NotNull BaseViewHolder helper, ListDialogItemBean item) {
            if (!TextUtil.isEmpty(EmojiFilterUtils.filterEmoji(item.getText()))) {
                helper.setText(R.id.tv_item_name, EmojiFilterUtils.filterEmoji(item.getText()));
            }
        }
    }

    /**
     * 设置属性
     *
     * @param collectState
     */
    public void setCollectState(boolean collectState) {
        if (collectState) {
            listData.get(0).setState(true);
        } else {
            listData.get(0).setState(false);
        }
        adapter.notifyDataSetChanged();
    }
}
